package com.evildog.workspace.life.application.controller;

import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.life.application.bo.NoteBO;
import com.evildog.workspace.life.application.bo.NoteListBO;
import com.evildog.workspace.life.application.convert.NoteConvert;
import com.evildog.workspace.life.application.model.NoteListParam;
import com.evildog.workspace.life.application.model.NoteParam;
import com.evildog.workspace.life.application.service.NoteService;
import com.evildog.workspace.life.application.vo.NoteListVO;
import com.evildog.workspace.life.application.vo.NoteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * note Controller
    笔记
 * </p>
 *
 * @author evildog
 * @since 2020-08-04
 */
@RestController
@RequestMapping("/notes")
@AllArgsConstructor
@Api(tags = "note相关接口")
public class NoteController {

    private NoteService noteService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<NoteVO> save(@RequestBody @Validated NoteParam noteParam) {
        NoteBO noteBO = noteService.save(NoteConvert.INSTANCE.convert(noteParam));
        return ResultGenerator.success(NoteConvert.INSTANCE.convert(noteBO));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(noteService.remove(id));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id,@RequestBody NoteParam noteParam) {
        return ResultGenerator.status(noteService.updateById(id, NoteConvert.INSTANCE.convert(noteParam)));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情接口")
    public Result<NoteVO> getById(@PathVariable("id") Long id) {
        NoteBO noteBO = noteService.getDetailById(id);
        return ResultGenerator.success(NoteConvert.INSTANCE.convert(noteBO));
    }

    @GetMapping
    @ApiOperation(value = "表List查询接口")
    public Result<List<NoteListVO>> getList(NoteListParam noteListParam){
        List<NoteListBO> noteListBO = noteService.getList(
                        QueryConditionUtils.getQueryCondition(noteListParam),
                        NoteConvert.INSTANCE.convert(noteListParam));
        return ResultGenerator.success(NoteConvert.INSTANCE.convertVoList(noteListBO));
    }

}